package cn.source.system.service.impl;

import java.util.List;
import java.util.Objects;

import cn.source.common.exception.ServiceException;
import cn.source.common.utils.DateUtils;
import cn.source.system.domain.CmsTool;
import cn.source.system.domain.vo.CmsCategoryToolVo;
import cn.source.system.domain.vo.CmsToolVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import cn.source.system.mapper.CmsToolCategoryMapper;
import cn.source.system.domain.CmsToolCategory;
import cn.source.system.service.ICmsToolCategoryService;

/**
 * 工具分类Service业务层处理
 * 
 * @author sourcebyte.vip
 * @date 2024-12-29
 */
@Service
public class CmsToolCategoryServiceImpl implements ICmsToolCategoryService 
{
    @Autowired
    private CmsToolCategoryMapper cmsToolCategoryMapper;

    /**
     * 查询工具分类
     * 
     * @param id 工具分类主键
     * @return 工具分类
     */
    @Override
    public CmsToolCategory selectCmsToolCategoryById(Long id)
    {
        return cmsToolCategoryMapper.selectCmsToolCategoryById(id);
    }

    /**
     * 查询工具分类列表
     * 
     * @param cmsToolCategory 工具分类
     * @return 工具分类
     */
    @Override
    public List<CmsToolCategory> selectCmsToolCategoryList(CmsToolCategory cmsToolCategory)
    {
        return cmsToolCategoryMapper.selectCmsToolCategoryList(cmsToolCategory);
    }

    /**
     * 新增工具分类
     * 
     * @param cmsToolCategory 工具分类
     * @return 结果
     */
    @Override
    public int insertCmsToolCategory(CmsToolCategory cmsToolCategory)
    {
        _validateCategoryCode(cmsToolCategory.getCode(), null);
        cmsToolCategory.setCreateTime(DateUtils.getNowDate());
        return cmsToolCategoryMapper.insertCmsToolCategory(cmsToolCategory);
    }

    /**
     * 修改工具分类
     * 
     * @param cmsToolCategory 工具分类
     * @return 结果
     */
    @Override
    public int updateCmsToolCategory(CmsToolCategory cmsToolCategory)
    {
        _validateCategoryCode(cmsToolCategory.getCode(), cmsToolCategory.getId());
        cmsToolCategory.setUpdateTime(DateUtils.getNowDate());
        return cmsToolCategoryMapper.updateCmsToolCategory(cmsToolCategory);
    }

    /**
     * 校验工具code是否存在
     * @param code 分类code
     * @param id 分类id
     */
    private void _validateCategoryCode(String code, Long id) {
        // 根据code查询数据信息
        CmsToolCategory localCategory = new CmsToolCategory();
        localCategory.setCode(code);
        List<CmsToolCategory> cmsToolCategories = selectCmsToolCategoryList(localCategory);
        // 新增
        if(id == null) {
            if(!cmsToolCategories.isEmpty()) {
                throw new ServiceException("该分类已经存在");
            }
        } else {
            // 取第一个分类是否与当前分类一致
            if (!cmsToolCategories.isEmpty()) {
                CmsToolCategory firstCategory = cmsToolCategories.get(0);
                if (null != firstCategory && !Objects.equals(firstCategory.getId(), id)) {
                    throw new ServiceException("该分类已经存在");
                }
            }
        }

    }

    /**
     * 批量删除工具分类
     * 
     * @param ids 需要删除的工具分类主键
     * @return 结果
     */
    @Override
    public int deleteCmsToolCategoryByIds(Long[] ids)
    {
        return cmsToolCategoryMapper.deleteCmsToolCategoryByIds(ids);
    }

    /**
     * 删除工具分类信息
     * 
     * @param id 工具分类主键
     * @return 结果
     */
    @Override
    public int deleteCmsToolCategoryById(Long id)
    {
        return cmsToolCategoryMapper.deleteCmsToolCategoryById(id);
    }

    @Override
    public List<CmsCategoryToolVo> selectCategoryToolList(CmsTool cmsTool) {
        return cmsToolCategoryMapper.selectCategoryToolList(cmsTool);
    }
}
